package com.other.src.main.java.com.kuang.readwritelock;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class a {
    public static void main(String[] args) {

//        Function function = new Function<String,String>() {
//
//            @Override
//            public String apply(String o) {
//                return o;
//            }
//        };

        //lambda简化，连参数类型都不制定了。

        //判断
        Predicate<String> predicate = (str) -> {
            return str.isEmpty();
        };
        // 消费者 只有输入，没有返回值
        Consumer<String> consumer = (str)->{
            System.out.println(str);
        };
        // 提供者 没有输入，只有返回值
        Supplier<String> supplier = () -> {
            System.out.println("get()");
          return "hello word";
        };
        System.out.println(supplier.get());
        Function function = (str)->{
            return  str;
        };

        // 工具类，输出输入的值

        System.out.println(function.apply("abc"));

        int i1 = Runtime.getRuntime().availableProcessors();
        System.out.println(i1);
        MyCacheLock myCache = new MyCacheLock();

        for (int i = 0; i <6; i++) {
            final int temp = i;
            new Thread(()->{

                myCache.put(temp+"",temp+"");
            },String.valueOf(i)).start();
        }


        for (int i = 0; i <6; i++) {
            final int temp = i;
            new Thread(()->{

                myCache.get(temp+"");
            },String.valueOf(i)).start();
        }




    }
}

//自定义缓存
class MyCacheLock {
    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private volatile Map<String, Object> map = new HashMap<>();

    public void put(String key, Object value) {
        readWriteLock.writeLock().lock();

        try {
            System.out.println(Thread.currentThread().getName() + "写入" + key);
            map.put(key, value);
            System.out.println(Thread.currentThread().getName() + "写入 ok" );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.writeLock().unlock();
        }

    }

    public void get(String key) {
        readWriteLock.readLock().lock();

        try {
            System.out.println(Thread.currentThread().getName() + "读取" + key);
            Object o = map.get(key);
            System.out.println(Thread.currentThread().getName() + "读取 ok" );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readWriteLock.readLock().unlock();

        }

    }
}

//自定义缓存
class MyCache {
    private volatile Map<String, Object> map = new HashMap<>();

    public void put(String key, Object value) {

        try {
            System.out.println(Thread.currentThread().getName() + "写入" + key);
            map.put(key, value);
            System.out.println(Thread.currentThread().getName() + "写入 ok" );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

    }

    public void get(String key) {

        try {
            System.out.println(Thread.currentThread().getName() + "读取" + key);
            Object o = map.get(key);
            System.out.println(Thread.currentThread().getName() + "读取 ok" );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }

    }
}